package com.wzy.service.impl;

import com.wzy.entity.Message;
import com.wzy.mapper.MessageMapper;
import com.wzy.service.MessageService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wzy
 * @since 2021-02-09
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements MessageService {

    @Resource
    private MessageMapper messageMapper;
    //存放迭代找出的所有子代的集合
    private List<Message> tempReplys = new ArrayList<>();

    @Override
    public List<Message> listMessage() {
//  第一步查询顶级留言
        List<Message> messages=this.messageMapper.findMessageNotParent();
//        遍历每一个顶级留言 为了找出每一个的回复
        for (Message message : messages) {
            Long topMessageId=message.getMessageId();
            String parentNickname=message.getNickname();
//            一级回复的储存集合
            List<Message> firstReply=this.messageMapper.findFirstReply(topMessageId);
//            查询一级回复
            combineFirstReply(firstReply,parentNickname);
//            设置顶级评论的回复集合
            message.setReplyMessages(tempReplys);
//            遍历结束将tempReplys初始化
            tempReplys=new ArrayList<>();
        }

        return messages;
    }

    private void combineFirstReply(List<Message> firstReply,String parentNickname){
        if(firstReply.size()>0){
            for (Message firstMessage : firstReply) {
                Long firstMessageId=firstMessage.getMessageId();
                String firstNickname=firstMessage.getNickname();
//                设置一级回复的父昵称
                firstMessage.setParentNickname(parentNickname);
//                firstMessage.setNickname(firstNickname);
                tempReplys.add(firstMessage);
//                查询二级回复
                combineSecondReply(firstMessageId,firstNickname);
            }
        }
    }

    private void combineSecondReply(Long firstMessageId,String parentNickname){
//      根据一级留言的id找到二级回复
        List<Message> secondReply=this.messageMapper.findSecondReply(firstMessageId);
        if(secondReply.size()>0){
            for (Message secondMessage : secondReply) {
                Long secondMessageId=secondMessage.getMessageId();
                String secondNickname=secondMessage.getNickname();
//                设置一级回复的父昵称
                secondMessage.setParentNickname(parentNickname);
//                secondMessage.setNickname(secondNickname);
                tempReplys.add(secondMessage);
//                查询二级回复
                combineSecondReply(secondMessageId,secondNickname);
            }
        }
    }



    @Override
    public int append(Message message) {
//        如果parent_messageId！=-1的话就说明他有父亲
        long parent_messageId=message.getParentMessageId();
        if(parent_messageId!=-1){
            message.setParentMessage(this.messageMapper.findByParentsId(parent_messageId));
        }else{
            message.setParentMessage(null);
        }

        return this.messageMapper.insert(message);
    }
}
